﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.Components;
using Slja.BLL;
using System.Xml;
using Slja.Web.Common;
using System.Text;
using Slja.Model;

namespace FrameWork.web.Web.Project
{
    public partial class OrderList : System.Web.UI.Page
    {
        protected string ViewType
        {
            get {
                if (Request.QueryString["ViewType"] == null)
                {
                    return "";
                }
                return Request.QueryString["ViewType"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["roleid"]) && !string.IsNullOrEmpty(Request.QueryString["title"]))
                {
                   
                    BindData();
                }
            }
        }
        private void BindData()
        {
            int RoleID = int.Parse(Request.QueryString["roleid"]); //Common.Get_UserID;
            string Title = Request.QueryString["title"];

            OrderBLL bll = new OrderBLL();
            string strSQLWhere = GetOrderTitleWhere(RoleID, Title);
            string strOrderby = Orderfld +" "+ (OrderType==0?"ASC":"DESC");            
            int pageSize = Pager1.PageSize;
            int curPage = Pager1.CurrentPageIndex;
            int recordCount;

            List<OrderExtModel> list = bll.GetModelExtList(strSQLWhere, strOrderby, pageSize, curPage, out recordCount);
            GridView1.DataSource = list;
            GridView1.DataBind();
            this.Pager1.RecordCount = recordCount;         
        }
        public string GetOrderTitleWhere(int RoleID,string Title)
        {
            XmlDocument xd = XmlDoc.GetOrderListViewDoc();
            string xpath = "/listview/view[@RoleID='" + RoleID + "']/title[@Caption='" + Title + "']/section";
            XmlNodeList nds = xd.SelectNodes(xpath);
            XmlNode nd = null;
            StringBuilder sb = new StringBuilder(64);
            //if (ViewType.ToUpper() != "PUBLIC")
            //{
            //    sb.Append(" CurOpUserID=" + Common.Get_UserID);
            //    sb.Append(" and ");
            //}
            sb.Append("1=0");
            #region 循环查条件
            for (int i = 0; i < nds.Count; i++)
            {
                sb.Append(" OR (1=1");
                nd = nds[i];
                string regular = "";
                if (nd["PreSaleState"] != null)
                {
                    regular = nd["PreSaleState"].Attributes["Regular"].Value;
                    if (regular == "1")
                    {
                        sb.AppendFormat(" AND ({0}&{1}={1} and {0}|{1}={1})", "PreSaleState", nd["PreSaleState"].InnerText);
                    }                   
                    else if (regular == "2")
                    {
                        sb.AppendFormat(" AND ({0}&{1}={1})", "PreSaleState", nd["PreSaleState"].InnerText);
                    }                  
                }
                if (nd["BackState"] != null)
                {
                    regular = nd["BackState"].Attributes["Regular"].Value;
                    if (regular == "1")
                    {
                        sb.AppendFormat("  AND ({0}&{1}={1} and {0}|{1}={1})", "BackState", nd["BackState"].InnerText);
                    }
                    else if (regular == "2")
                    {
                        sb.AppendFormat("  AND ({0}&{1}={1})", "BackState", nd["BackState"].InnerText);
                    }
                    else if (regular == "-1")
                    {
                        sb.AppendFormat("  AND ({0}&{1}<>{1})", "BackState", nd["BackState"].InnerText);
                    }
                }
                if (nd["Log"] != null)
                {
                    string optype = nd["Log"].Attributes["OpType"].Value.ToLower();
                    string userid = nd["Log"].Attributes["UserID"].Value.ToLower();
                    string nextstate = nd["Log"].Attributes["NextState"].Value.ToLower();
                    if(userid=="self")
                    {
                        userid = Common.Get_UserID.ToString();
                    }
                    sb.AppendFormat(" AND EXISTS(select 1 from biz_OrderLog lg where a.ID=lg.OrderID and OpType='{0}' and UserID='{1}' and NextState='{2}' )", optype, userid, nextstate);
                }//
                if (nd["CurOpUserID"] != null)
                {
                    string curopuserid = nd["CurOpUserID"].InnerText.ToLower();
                    if (curopuserid == "self")
                    {
                        curopuserid = Common.Get_UserID.ToString();
                    }
                    sb.AppendFormat(" AND CurOpUserID={0}", curopuserid);
                }
                sb.Append(")");
            }//for
            #endregion
            //sb.Append(")");
            return sb.ToString();

        }
        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
           BindData();
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        private string SearchTerms
        {
            get
            {
                if (ViewState["SearchTerms"] == null)
                    ViewState["SearchTerms"] = " Where U_Status<>2";
                return (string)ViewState["SearchTerms"];
            }
            set { ViewState["SearchTerms"] = value; }
        }

        #region "排序"
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Orderfld == e.SortExpression)
            {
                if (OrderType == 0)
                {
                    OrderType = 1;
                }
                else
                {
                    OrderType = 0;
                }
            }
            Orderfld = e.SortExpression;
            BindData();
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string Orderfld
        {
            get
            {
                if (ViewState["sortOrderfld"] == null)

                    ViewState["sortOrderfld"] = "createtime"; ;

                return (string)ViewState["sortOrderfld"];
            }
            set
            {
                ViewState["sortOrderfld"] = value;
            }
        }

        /// <summary>
        /// 排序类型 1:降序 0:升序
        /// </summary>
        public int OrderType
        {
            get
            {
                if (ViewState["sortOrderType"] == null)
                    ViewState["sortOrderType"] = 1;

                return (int)ViewState["sortOrderType"];
            }

            set { ViewState["sortOrderType"] = value; }

        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell var in e.Row.Cells)
                {
                    if (var.Controls.Count > 0 && var.Controls[0] is LinkButton)
                    {
                        string Colume = ((LinkButton)var.Controls[0]).CommandArgument;
                        if (Colume == Orderfld)
                        {

                            LinkButton l = (LinkButton)var.Controls[0];
                            l.Text += string.Format("<img src='{0}' border='0'>", (OrderType == 0) ? Page.ResolveUrl("~/Manager/images/sort_asc.gif") : Page.ResolveUrl("~/Manager/images/sort_desc.gif"));
                            //Image Img = new Image();
                            //SortDirection a = GridView1.SortDirection;
                            //Img.ImageUrl = (a == SortDirection.Ascending) ? "i_p_sort_asc.gif" : "i_p_sort_desc.gif";
                            //var.Controls.Add(Img);
                        }
                    }
                }
            }
        }
        #endregion
    }
}